/**
 * 承包方设置 编辑页
 *
 * 作者 admin
 * 日期 2024-07-29 14:19:02
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#baseContractor-edit'),
        validate = layui.validate,
        tableIns;

    let tenantId = POPUP_DATA.tenantId

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        console.log("POPUP_DATA.tenantId"+tenantId)
        if (baseContractorData) {
            form.val("baseContractor-edit-form", {
                "contractorId": baseContractorData.contractorId,
                "tenantId": baseContractorData.tenantId,
                "contractorCode": baseContractorData.contractorCode,
                "contractorName": baseContractorData.contractorName,
                "shortName": baseContractorData.shortName,
                "contractorPinyin": baseContractorData.contractorPinyin,
                "attribute": baseContractorData.attribute,
                "contacts": baseContractorData.contacts,
                "contactsPhone": baseContractorData.contactsPhone,
                // "status": baseContractorData.status,
            });

            //根基后台status值渲染开关
            if(baseContractorData.status === '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
                form.render('checkbox');
            }

        }else{
            form.val("baseContractor-edit-form", {
                "tenantId": tenantId
            });
            $view.find('input[type=checkbox]').attr("checked","true");
            form.render('checkbox');
        }

    }

    form.on('submit(baseContractor-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    // febs.reloadPage = function(curr) {
    //     var _curr = $(".layui-laypage-em").next().html();
    //     if (!!curr) {
    //         _curr = curr;
    //     }
    //     tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    // }

    function addOrUpdate(data) {
        if (data.contractorId && data.contractorId.length > 0) {
            febs.modal.confirm('修改承包商', '确定修改承包商？', function () {
                febs.post(ctx + 'baseContractor/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseContractor').find('#query').trigger('click');
                });
            });
        } else {
            febs.modal.confirm('新增承包商', '确定新增承包商？', function () {
                febs.post(ctx + 'baseContractor', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseContractor').find('#query').trigger('click');
                });
            });

        }
    }
});